//
//  SettingHomeViewController.m
//  VietQuan
//
//  Created by hainguyen on 11/13/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "SettingHomeViewController.h"
#import "TwitterPoster.h"
#import "FBPoster.h"
#import "FBSession.h"
#import "FBUtilities.h"

@implementation SettingHomeViewController
@synthesize loginFacebookBtn;
@synthesize loginTwitterBtn;
@synthesize fbUsernameLabel;
@synthesize twitNameLabel;
@synthesize twitterPoster;
@synthesize facebookPoster;
@synthesize fbUtilities;

- (void)dealloc{
    [fbUtilities release];
    [twitterPoster release];
    [facebookPoster release];
    [loginFacebookBtn release];
    [loginTwitterBtn release];
    [fbUsernameLabel release];
    [twitNameLabel release];
    [super dealloc];
}

- (IBAction)loginFacebook{
    FBSession *session = [FBSession session];
    if (session == nil) {
        if (fbUtilities == nil) {
            fbUtilities = [[FBUtilities alloc] init];
        }
        [fbUtilities facebookLogin:self];
    } else{
        [session logout];
    }
}

- (IBAction)loginTwitter{
    if (self.twitterPoster == nil) {
        self.twitterPoster = [[TwitterPoster alloc] init];
    }
    [self.twitterPoster login:self];
}

#pragma mark -
#pragma mark TwitterPoster Delegate

- (void) getUserInfoTwitter{
    [twitNameLabel setText:self.twitterPoster.username];
}

- (void) logOff{
    
}

#pragma mark -
#pragma mark FBUtilities Delegate

- (void)facebookFinishedLogin{
    [loginFacebookBtn setSelected:YES];
}

- (void)getUserInfo:(id)result{
    NSArray* users = result;
    NSDictionary* user = [users objectAtIndex:0];
    NSString* name = [user objectForKey:@"name"];
    fbUsernameLabel.text = name;
}

- (void) logout{
    [loginFacebookBtn setSelected:NO];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
